/******************************************************************************
 * SVN­Versioning-Information:
 * ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
 * $LastChangedRevision: 6 $
 * $LastChangedBy: c.a.duevel $
 * $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
 * $HeadURL: http://robofight.googlecode.com/svn/trunk/Server/src/de/hft_stuttgart/botwar/server/model/RobotConfiguration.java $
 ******************************************************************************/
package de.hft_stuttgart.botwar.server.model;

import de.hft_stuttgart.botwar.common.models.RobotInfo;
import de.hft_stuttgart.botwar.database.Player;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *
 * @author pmv-mail@gmx.de
 */
@Entity
@Table(name = "tblRobot")
public class RobotConfiguration implements Serializable {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(name = "Name", nullable = false)
    private String name;
    @ManyToOne
    @JoinColumn(name = "Username", nullable = false)
    private Player player;
    @ManyToOne
    @JoinColumn(name = "Chassis", nullable = false)
    private Chassis chassis;
    @ManyToOne
    @JoinColumn(name = "Shield", nullable = false)
    private Shield shield;
    @ManyToOne
    @JoinColumn(name = "Armament", nullable = false)
    private Armament armament;

    /**
     * This constructor exists only because the JPA requires it.
     */
    @Deprecated
    public RobotConfiguration() {
    }

    /**
     * A robot configuration consits of a chassis, a shield and an armament.
     * @param name Name of this configuration.
     * @param player Player who owns this configuration.
     * @param chassis Chassis of this configuration.
     * @param shield Shield of this configuration.
     * @param armament Armament of this configuration.
     */
    public RobotConfiguration(final String name,
            final Player player,
            final Chassis chassis,
            final Shield shield,
            final Armament armament) {
        setName(name);
        this.player = player;
        this.chassis = chassis;
        this.shield = shield;
        this.armament = armament;
        checkConstraints();
    }

    /**
     *
     * @return id of this configuration.
     */
    public int getId() {
        return id;
    }

    /**
     * @return Name of this configuration.
     */
    public String getName() {
        return name;
    }

    /**
     * @param name The name of this configuration.
     */
    public void setName(final String name) {
        this.name = name;
    }

    /**
     *
     * @return The armament of this configuration
     */
    public Armament getArmament() {
        return armament;
    }

    /**
     *
     * @param armament The armament of this configuration
     */
    public void setArmament(final Armament armament) {
        this.armament = armament;
        checkConstraints();
    }

    /**
     *
     * @return The chassis of this configuration
     */
    public Chassis getChassis() {
        return chassis;
    }

    /**
     *
     * @param chassis the chassis of this configuration
     */
    public void setChassis(final Chassis chassis) {
        this.chassis = chassis;
        checkConstraints();
    }

    /**
     *
     * @return Player who owns this configuration.
     */
    public Player getPlayer() {
        return player;
    }

    /**
     *
     * @param player who owns this configuration
     */
    public void setPlayer(final Player player) {
        this.player = player;
        checkConstraints();
    }

    /**
     *
     * @return The shield of this configuration.
     */
    public Shield getShield() {
        return shield;
    }

    /**
     *
     * @param shield The shield of this configuration.
     */
    public void setShield(final Shield shield) {
        this.shield = shield;
        checkConstraints();
    }

    /**
     *
     * @return The cost of this configuration
     */
    public int getCost() {
        return chassis.getCost() + shield.getCost() + armament.getCost();

    }

    private void checkConstraints() {
        if (chassis.getWeight() < shield.getWeight() + armament.getWeight()) {
            throw new IllegalArgumentException("Weight of shield and armament" +
                    " to high for chassis!");
        }
    }

    /**
     *
     * @return the RobotInfo object of this configuration.
     */
    public RobotInfo getInfo() {
        return new RobotInfo(this.id,
                             this.player.getUsername(),
                             this.name,
                             this.armament.getInfo(),
                             this.chassis.getInfo(),
                             this.shield.getInfo());
    }

    /**
     * @return Needed level for this robot.
     */
    public int getLevelRank() {
        int levelRank = Math.max(armament.getLevelRank()
                , chassis.getLevelRank());
        return Math.max(levelRank, shield.getLevelRank());
    }
}
